package b3.widget.guinness
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.net.*;
	import flash.events.*;

	public class GuinnessItem extends Sprite
	{
		private var pictLdr:Loader;
		
		public function GuinnessItem(worldRecord:RecordInfo, personalRecord:RecordInfo)
		{
			super();
			var guinessConfig:GuinnessConfig = Config.guinnessConfigs[worldRecord.type + "_" + worldRecord.recordIndex];
			if (guinessConfig != null)
			{
				//图标
				var IconImage:Class = ResourceHelper.getInstance().getClassByName(guinessConfig.iconImage);
				var iconImage:Sprite = new IconImage()
				iconImage.x = 45 - iconImage.width / 2;
				iconImage.y = 20 - iconImage.height / 2;
				addChild(iconImage);
				//保持者头像
				if (worldRecord.figureurl != null)
				{
					pictLdr = new Loader();
					var pictURLReq:URLRequest = new URLRequest(worldRecord.figureurl);
					pictLdr.load(pictURLReq);
					pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onUserPicLoaded);
					addChild(pictLdr);
				}
				//标题名字
				var titleText:TextField = new TextField();
				titleText.selectable = false;
	        	titleText.autoSize = TextFieldAutoSize.CENTER;
	        	if (guinessConfig.name.length > 6)
	        		titleText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000, true);
	        	else
					titleText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000, true);
				titleText.x = 150;
				titleText.y = 40;
				titleText.text = guinessConfig.name;
				addChild(titleText);
				//描述文字
				var description:TextField = new TextField();
				description.selectable = false;
	        	description.autoSize = TextFieldAutoSize.LEFT;
	        	description.multiline = true;
	        	description.wordWrap = true;
	        	description.width = 220;
				description.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000);
				description.x = 0;
				description.y = 85;
	        	var formatDescrip:String = guinessConfig.description1;
	        	formatDescrip = formatDescrip.replace(/recordValue1/, "<b><i><font color='#FF0000' size='18'> " + worldRecord.recordValue + " </font></i></b>");
	        	formatDescrip = formatDescrip.replace(/holderName1/, "<b><font color='#0000FF'> " + worldRecord.holderName.substr(0,4) + " </font></b>");
	        	formatDescrip = formatDescrip.replace(/recordDate1/, "<b><i><font color='#00AA00'> " + worldRecord.recordDate.slice(0, -3) + " </font></i></b>");
	        	description.htmlText = formatDescrip;
				addChild(description);
				//描述文字2
				var description2:TextField = new TextField();
				description2.selectable = false;
	        	description2.autoSize = TextFieldAutoSize.LEFT;
	        	description2.multiline = true;
	        	description2.wordWrap = true;
	        	description2.width = 220;
				description2.defaultTextFormat = new TextFormat("微软雅黑", 10, 0x000000);
				description2.x = 0;
				description2.y = description.y + description.height;
				if (personalRecord != null)
				{
		        	formatDescrip = guinessConfig.description2;
		        	formatDescrip = formatDescrip.replace(/recordValue2/, "<i> " + personalRecord.recordValue + " </i>");
		        	formatDescrip = formatDescrip.replace(/holderName2/, "<i> " + personalRecord.holderName.substr(0,4) + " </i>");
		        	formatDescrip = formatDescrip.replace(/recordDate2/, "<i> " + personalRecord.recordDate.slice(0, -3) + " </i>");
		  		}
		  		else
		  		{
		  			formatDescrip = "（尚未有我的纪录）";
		  		}
	        	description2.htmlText = formatDescrip;
				addChild(description2);
			}
		}
		
		private function onUserPicLoaded(event:Event):void
		{
			pictLdr.x = 150 - pictLdr.width / 2;
			pictLdr.y = 10 - pictLdr.height / 2;
		}
		
	}
}